package com.ptswitch.material.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class IRIGBUtil {
    public static String encoding = "UTF-8";
    private static final Logger logger = LoggerFactory.getLogger(IRIGBUtil.class);
    public static final int frequence = 2500;
    public static final int codeNumPerSecond = 100;
    public static final int bitNumPerCode = frequence / codeNumPerSecond;
    public static final int unit = 10;
    public static final int bitNumWhenZero = bitNumPerCode * 2 / unit;
    public static final int bitNumWhenOne = bitNumPerCode * 5 / unit;
    public static final int bitNumWhenP = bitNumPerCode * 8 / unit;

    public static String svToPCode(String svStr, double threshold) {
        //get binary
        StringBuffer binaryBuffer = new StringBuffer();
        char lastBinary = '\0';
        char curBinary = '\0';
        String[] svLines = svStr.split("\n");
        for (String svLine : svLines) {
            try {
                String[] fields = svLine.split("[ ,]");
                double d = Double.parseDouble(fields[1]);
                curBinary = (d > threshold ? '1' : '0');
                if (curBinary == '0' && lastBinary == '1') {
                    binaryBuffer.append(',');
                }
                binaryBuffer.append(curBinary);
                lastBinary = curBinary;
            } catch (NumberFormatException e) {
                logger.debug(svLine);
            } catch (ArrayIndexOutOfBoundsException e) {
                logger.debug(svLine);
            }
        }
        //decode
        char[] binaryArray = binaryBuffer.toString().toCharArray();
        StringBuffer codeBuffer = new StringBuffer();
        int countZero = 0;
//            int countOne = 0;
        char curCode = '\0';
        char lastCode = '\0';
        for (int i = 0; i < binaryArray.length; i++) {
            char cur = binaryArray[i];
            if (cur == '1') {
//                    countOne++;
            } else if (cur == '0') {
                countZero++;
            } else if (cur == ',') {//end mayuan
                if (countZero > bitNumWhenP - 2 && countZero < bitNumWhenP + 2) {
                    curCode = 'P';
                } else if (countZero > bitNumWhenOne - 2 && countZero < bitNumWhenOne + 3) {
                    curCode = '1';
                } else if (countZero > bitNumWhenZero - 2 && countZero < bitNumWhenZero + 2) {
                    curCode = '0';
                } else {
                    curCode = 'e';
                }
//                    countOne = 0;
                countZero = 0;//reset
                if (curCode == 'P' && lastCode == 'P') {
                    codeBuffer.append("<br />PP");
                    curCode = '\0';//fix append(,P) when lastCode=P
                } else if (curCode == 'P' && lastCode != 'P') {
                    //wait
                } else {
                    if (lastCode == 'P') {
                        codeBuffer.append(",P");
                    }
                    codeBuffer.append(curCode);
                }
                lastCode = curCode;
            } else {
            }

        }
        return codeBuffer.toString();
    }

    public static String[] pcodeToBinary(String pcodeStr) {
        StringBuffer binaryBuffer = new StringBuffer("@00 ");
        int count = 0;
        for (char code : pcodeStr.toCharArray()) {
            if (code == '0') {
                binaryBuffer.append("00 ");
                count++;
            } else if (code == '1') {
                binaryBuffer.append("01 ");
                count++;
            } else if (code == 'P') {
                binaryBuffer.append("02 ");
                count++;
            }
        }
        return new String[] { binaryBuffer.toString(), String.valueOf(count) };
    }

}
